package com.cdp.web.controller.system;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysStone;
import com.cdp.system.dto.StoneDTO;
import com.cdp.system.service.ISysStoneService;
import com.cdp.system.vo.IdsVO;
import com.cdp.system.vo.SandFineneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.cdp.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 石头信息
 */
@RestController
@RequestMapping("/system/stone")
public class SysStoneController {


    @Autowired
    private ISysStoneService sysStoneService;


    @GetMapping("/listStones")
    public AjaxResult listStones() {
        List<SysStone> listStones = sysStoneService.listStones();
        return AjaxResult.success(listStones);
    }





    /**
     * 石头表格模糊查询
     */
    @PostMapping("listMistedStones")
    public AjaxResult listMistedStones(@RequestBody StoneDTO stoneDTO) {

        // 执行业务逻辑
        try {

            return sysStoneService.listMistedStones(stoneDTO);
        } catch (Exception e) {
            log.error("查询执行时发生错误，参数为: {}", stoneDTO, e);
            return AjaxResult.error("系统内部错误，请稍后重试。");
        }
    }

    /**
     * 石id模糊查询
     */

    @GetMapping("/queryStoneIdsById")
    public AjaxResult queryStoneIdsById(@RequestParam(required = false) Integer id) {
        List<IdsVO> idsVOS =sysStoneService.querySandIdsById(id);

        List<SandFineneVO> sandFinenessVOList = idsVOS.stream()
                .flatMap(idsVO -> idsVO.getIds().stream()) // 提取 ids 列表
                .map(idValue -> {
                    SandFineneVO vo = new SandFineneVO();
                    vo.setValue(String.valueOf(idValue)); // 将 Integer 转为 String
                    vo.setLabel(String.valueOf(idValue));
                    return vo;
                })
                .collect(Collectors.toList());

        return AjaxResult.success(sandFinenessVOList);

    }




}
